﻿using GrapeCity.ActiveReports;
using GrapeCity.ActiveReports.Export;
using GrapeCity.ActiveReports.Extensibility.Rendering;
using GrapeCity.ActiveReports.Rendering.IO;
using System;
using System.Collections.Specialized;
using System.Data.OleDb;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Export
{
	public partial class ExportForm : Form
	{

		private const string _pdf = "pdf";
		private const string _xlsx = "xlsx";
		private const string _mht = "mht";
		private const string _rtf = "rtf";
		private const string _csv = "csv";
		private const string _docx = "docx";
		private const string _json = "json";
		

		public ExportForm()
		{
			InitializeComponent();
			FillReportsNames();
		}

		private void ExportForm_Load(object sender, EventArgs e)
		{
			reportsNames.SelectedIndex = 0;
			exportTypes.SelectedIndex = 0;
		}

		private void FillReportsNames()
		{
			var directoryInfo = new DirectoryInfo("../../../../Reports/");
			foreach (var file in directoryInfo.GetFiles())
			{
				reportsNames.Items.Add(file.Name);
			}
		}
		private void OnLocateDataSource(object sender, LocateDataSourceEventArgs args) {

			//var connStr = "Driver={MySQL ODBC 8.0 UNICODE Driver};Server=db.rservice.cn;Port=3306;Database=dev_vehicle_1.1;User=dev_publish;Password=r@h@20200410@801;Option=3;";
			//var conn = new OleDbConnection(connStr);
			//conn.Open();
			//var cmd = new OleDbCommand("select * from common_reports", conn);
			//var dataReader = cmd.ExecuteReader();
			//conn.Close();
			//if (args.DataSet.Name == "Years") // ObjectsReport :bind collection to report
			//{
			//	args.Data = _dataList;
			//} else if (args.DataSet.Name == "Movies") // SubObjectsReport :bind subcollection to subreport
			//  {
			//	var yearReleased = 1970;
			//	foreach (var param in args.Parameters)
			//		if (param.Name == "YearReleased") {
			//			yearReleased = int.Parse(param.Value.ToString());
			//			break;
			//		}
			//	for (int i = 0; i < _dataList.Count; i++)
			//		if (_dataList[i].YearReleased == yearReleased) {
			//			args.Data = _dataList[i].Movies;
			//			break;
			//		}
			//}
			var  dd= DBHelper.GetTable("select `code`,`name`,sp_name from common_reports");
			args.Data = dd;
		}

		private void exportButton_Click(object sender, EventArgs e)
		{
			var reportName = (string)reportsNames.SelectedItem;
			var exportType = (string)exportTypes.SelectedItem;
			#region SectionReport
			//IDocumentExportEx documentExportEx = null;
			//System.Xml.XmlTextReader xtr = new System.Xml.XmlTextReader(@"..\..\..\..\C#\Export\RdlReport1.rdlx");
			//var report = new SectionReport();
			////new FileInfo(@"..\..\..\..\C#\Export\RdlReport1.rdlx");
			////var 
			//report.LoadLayout(xtr);
			//report.Parameters.Add(new GrapeCity.ActiveReports.SectionReportModel.Parameter() { });
			//report.DataSource = DBHelper.GetTable("select * from common_reports where reportid='0455744a-7590-48ee-ad13-82dcad5fb9f4'");
			//report.Run();
			//var exporTypeLower = exportType.ToLower();
			//switch (exporTypeLower) {
			//	case _pdf:
			//		documentExportEx = new GrapeCity.ActiveReports.Export.Pdf.Section.PdfExport();
			//		break;
			//	case _xlsx:
			//		documentExportEx = new GrapeCity.ActiveReports.Export.Excel.Section.XlsExport() { FileFormat = GrapeCity.ActiveReports.Export.Excel.Section.FileFormat.Xlsx };
			//		break;
			//	case _rtf:
			//		documentExportEx = new GrapeCity.ActiveReports.Export.Word.Section.RtfExport();
			//		break;
			//	case _mht:
			//		documentExportEx = new GrapeCity.ActiveReports.Export.Html.Section.HtmlExport();
			//		break;
			//	case _csv:
			//		documentExportEx = new GrapeCity.ActiveReports.Export.Xml.Section.TextExport();
			//		break;
			//}

			//var fileName = Path.GetFileNameWithoutExtension(reportName);
			//var saveFileDialog = new SaveFileDialog() {
			//	FileName = fileName + GetSaveDialogExtension(exporTypeLower),
			//	Filter = GetSaveDialogFilter(exporTypeLower)
			//};


			//if (saveFileDialog.ShowDialog() != DialogResult.OK) {
			//	return;
			//}

			//exportButton.Enabled = false;
			//using (var stream = new FileStream(Path.Combine(saveFileDialog.FileName), FileMode.Create))
			//	documentExportEx.Export(report.Document, stream);
			//exportButton.Enabled = true; 
			#endregion
			#region old
			//IRenderingExtension renderingExtension = null;
			//NameValueCollection settings = null;
			//var exporTypeLower = exportType.ToLower();
			//switch (exporTypeLower) {
			//	case _pdf:
			//		renderingExtension = new GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension();
			//		settings = new GrapeCity.ActiveReports.Export.Pdf.Page.Settings();
			//		break;
			//	case _xlsx:
			//		renderingExtension = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension();
			//		ISettings excelSettings = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings() {
			//			FileFormat = GrapeCity.ActiveReports.Export.Excel.Page.FileFormat.Xlsx
			//		};
			//		settings = excelSettings.GetSettings();
			//		break;
			//	case _csv:
			//		settings = new GrapeCity.ActiveReports.Export.Text.Page.CsvRenderingExtension.Settings() {
			//			ColumnsDelimiter = ",",
			//			RowsDelimiter = "\r\n",
			//			QuotationSymbol = '"',
			//			Encoding = Encoding.UTF8
			//		};
			//		renderingExtension = new GrapeCity.ActiveReports.Export.Text.Page.CsvRenderingExtension();
			//		break;
			//	case _docx:
			//		renderingExtension = new GrapeCity.ActiveReports.Export.Word.Page.WordRenderingExtension();
			//		settings = new GrapeCity.ActiveReports.Export.Word.Page.Settings() { FileFormat = GrapeCity.ActiveReports.Export.Word.Page.FileFormat.OOXML };

			//		break;
			//	case _mht:
			//		renderingExtension = new GrapeCity.ActiveReports.Export.Html.Page.HtmlRenderingExtension();
			//		settings = new GrapeCity.ActiveReports.Export.Html.Page.Settings() { MhtOutput = true, OutputTOC = true, Fragment = false };

			//		break;
			//	case _json:
			//		settings = new GrapeCity.ActiveReports.Export.Text.Page.JsonRenderingExtension.Settings() { Formatted = true };
			//		renderingExtension = new GrapeCity.ActiveReports.Export.Text.Page.JsonRenderingExtension();
			//		break;
			//}
			////D:\workspace\新建文件夹\示例源码\Samples14-master\API\PageAndRDL\Export\C#\Export\RdlReport1.rdlx
			//var report = new PageReport(new FileInfo(@"..\..\..\..\C#\Export\RdlReport1.rdlx"));
			//report.Document.LocateDataSource += OnLocateDataSource;

			//var fileName = Path.GetFileNameWithoutExtension(reportName);
			//var saveFileDialog = new SaveFileDialog() {
			//	FileName = fileName + GetSaveDialogExtension(exporTypeLower),
			//	Filter = GetSaveDialogFilter(exporTypeLower)
			//};

			//if (saveFileDialog.ShowDialog() != DialogResult.OK) {
			//	return;
			//}

			//var outputDirectory = new DirectoryInfo(Path.GetDirectoryName(saveFileDialog.FileName));
			//var outputProvider = new FileStreamProvider(outputDirectory, Path.GetFileNameWithoutExtension(saveFileDialog.FileName));

			//outputProvider.OverwriteOutputFile = true;

			//exportButton.Enabled = false;
			//report.Document.Render(renderingExtension, outputProvider, settings);
			//exportButton.Enabled = true;
			#endregion

			Exportdemo();

		}
		private void Exportdemo() {
			// Provide the page report you want to render.
			System.IO.FileInfo rptPath = new System.IO.FileInfo(@"..\..\..\..\C#\Export\RdlReport1.rdlx");
			GrapeCity.ActiveReports.PageReport pageReport = new GrapeCity.ActiveReports.PageReport(rptPath);
			pageReport.Document.LocateDataSource += OnLocateDataSource;
			// Create an output directory.
			System.IO.DirectoryInfo outputDirectory = new System.IO.DirectoryInfo(@"C:\MyPDF");
			outputDirectory.Create();

			// Provide settings for your rendering output.
			GrapeCity.ActiveReports.Export.Pdf.Page.Settings pdfSetting = new GrapeCity.ActiveReports.Export.Pdf.Page.Settings();

			// Set the rendering extension and render the report.
			GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension pdfRenderingExtension = new GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension();
			FileStreamProvider outputProvider = new FileStreamProvider(outputDirectory, Path.GetFileNameWithoutExtension(outputDirectory.Name));

			// Overwrite output file if it already exists
			outputProvider.OverwriteOutputFile = true;

			pageReport.Document.Render(pdfRenderingExtension, outputProvider, pdfSetting);
		}

		private string GetSaveDialogFilter(string exportType)
		{
			return exportType + " files | *." + exportType;
		}

		private string GetSaveDialogExtension(string exportType)
		{
			return "." + exportType;
		}
	}
}
